;
(function($,win)
{
	/* @------------------------------------------------------------------------------------@ */
	$(document).ready(function()
	{
		debugger;
		var url = location.search;
		if (url.indexOf("?dealerUuid=") != -1) {
			var reqPath = basePath+'dealer/queryDealerById'+url;
			var sendBody = '{}';
			send(reqPath, "POST", "application/json", "json", sendBody, function(res)
			{
				if (res.result == 'success')
				{
					if (res.data != null)
					{
						debugger;
						var dealer = res.data;
						$("#uuid").val(dealer.dealerUuid);
						$("#dealer_name").html(dealer.dealerName);
						$("#dealer_shop").html(dealer.dealerShop);
						initDealerImageData(1,15);
					} else
					{
						alert("没有查找到相关数据");
					}
				}
			});
		}else if(url.indexOf("?dealerId=") != -1)
		{
			debugger;
			$("#dealerUuid").val(url.split('=')[1]);
		}else if(url.indexOf("?DealerImageUuid=") != -1)
		{
			debugger;
			var reqPath = basePath+'order/queryDealerImageById'+url.split('&')[0];
			var sendBody = '{}';
			send(reqPath, "POST", "application/json", "json", sendBody, function(res)
			{
				if (res.result == 'success')
				{
					if (res.data != null)
					{
						var DealerImage = res.data;
						$("#uuid").val(url.split('&')[1].split('=')[1]);
						$("#DealerImageUuid").val(DealerImage.DealerImageUuid);
						$("#description").val(DealerImage.description);
						queryHandle(DealerImage.handleUuid);
					} else
					{
						alert("没有查找到相关数据");
					}
				}
			});
		}
		else
		{
			//初始化列表
			initDealerImageData(1,15);
		}
		$('#addnew').click(function()
		{
			debugger;
			window.location.href = jumpPath + 'view/udwin/dealer/addDealerImage.jsp?dealerId='+$("#uuid").val();
		});

		// 查询
		$('#btn_search').bind('click', function()
		{
			initDealerImageData(1,15);
		});

		$('#tbn_save').bind('click', function()
		{
			saveDealerImage();
		});
		
		$('#tbn_update').bind('click', function()
		{
			updateDealerImage();
		});

	});// the end

	/* @-----------------------------------------------------------------------------------------@ */
})(jQuery, window);

function queryHandle(handleUuid)
{
	var requsetPath = basePath + "handle/queryList";
	send(requsetPath, "POST", "application/json", "json", '{}',
	function(resultData)
	{
		if (resultData.result == 'success')
		{
			$.each(resultData.data, function(i,n)
			{
				$("#handle_uuid").append("<option value='"+n.handleUuid+"'>"+n.handleName+"</option>");
			});
			if(null!=handleUuid)
			{
				$("#handle_uuid").val(handleUuid);
			}
		} else
		{
			alert(resultData.errorMsg);
		}
	});
}

function addFile(e)
{
	debugger;
	var id = e.parentElement.parentElement.getAttribute("id");
	$('#'+id).append('<li><input type="file" name="'+id+'"/><button type="button" class="btn btn-primary" onClick="clickHandler(this)">删除</button></li>');
}

function clickHandler(e){
	e.parentNode.remove();
}

function editDealerImage(DealerImageUuid)
{
	window.location.href = jumpPath + 'view/udwin/order/editDealerImage.jsp?DealerImageUuid='+DealerImageUuid+'&orderUuid='+$("#uuid").val();
}

function del(id)
{
	if (confirm("确定要删除吗？"))
	{
		var reqPath = basePath+'dealer/deleteDealerImage?dealerImageUuid='+id;
		send(reqPath, "POST", "application/json", "json", "{}", function(res)
		{
			if (res.result == 'success')
			{
				initDealerImageData(1,15);
			}
		});
	}
}

function checkDealerImage(imageUrl)
{
	 window.open (imageUrl,'newwindow','width='+(window.screen.availWidth-10)+',height='+(window.screen.availHeight-30)+ ',top=0,left=0,toolbar=no,menubar=no,scrollbars=no, resizable=no,location=no, status=no')
}

function initDealerImageData(pageNumber,pagesize)
{
	var reqPath = basePath+'dealer/queryDealerImagePage';
	var sendBody = '{"curPage": 1,"pageSize": 15,"dealerUuid":"'+$("#uuid").val()+'"}';
	var $tab = $('#DealerImageTables tbody').html("");

	
	var reqPath = basePath + 'dealer/queryDealerImagePage';
	var sendBody = '{"curPage": '+pageNumber+',"pageSize": '+pagesize+',"dealerUuid":"'+$("#uuid").val()+'"}';
	var $tab = $('#DealerImageTables tbody').html("");
	
	var requestData = $('#frm_DealerImage').serializeObject();
	requestData = JSON.stringify(requestData);
//	sendBody = sendBody+requestData.replace('{',',');

	send(reqPath, "POST", "application/json", "json", sendBody, function(res)
	{

		if (res.result == 'success')
		{

			if (res.data.data.length > 0)
			{
				var tpl_tr = $('#tpl_list').html(), rows = '';

				$.each(res.data.data, function(i,n)
				{
					var operation="<button onclick='checkDealerImage(\""+n.imageUrl+"\")' type='button' class='btn btn-primary'>查看</button>"+
								  "<button onclick='del(\""+n.dealerImageUuid+"\")' type='button' class='btn btn-primary'>删除</button>";
					rows += tpl_tr.replace(/\$imageurl/g, n.imageUrl)
							      .replace(/\$operation/, operation);
				});
				$tab.append(rows);

				// page
				pagefunction(initDealerImageData,res.data.totalPages,pagesize);

			} else
			{
				$tab.append('<tr><td colspan="7">没有查找到相关数据。</td></tr>');
			}
		}
	});

}

/**
 * 
 * @METHODNAME: saveLoginDealerImager
 * @METDHOD:
 * @Description: 添加登陆用户
 * @author Administrator
 * @date 2015年1月10日 下午5:37:52
 */
function saveDealerImage()
{
	
	var reqDealerImagetPath = basePath + "order/addDealerImage";
	var requestData = $('#frm_DealerImage').serializeObject();
	requestData = JSON.stringify(requestData);
	send(reqDealerImagetPath, "POST", "application/json", "json", requestData,
	function(resultData)
	{

		if (resultData.result == 'success')
		{
			// 跳转到列表
			window.location = jumpPath
					+ 'view/udwin/order/listDealerImage.jsp?orderUuid='+$("#uuid").val();
		} else
		{
			alert(resultData.errorMsg);
		}
	});
}


function updateDealerImage()
{
	debugger;
	var reqDealerImagetPath = basePath + "order/updateDealerImage";
	var requestData = $('#frm_DealerImage').serializeObject();
	requestData = JSON.stringify(requestData);
	send(reqDealerImagetPath, "POST", "application/json", "json", requestData,
	function(resultData)
	{
		debugger;
		if (resultData.result == 'success')
		{
			// 跳转到列表
			window.location = jumpPath
					+ 'view/udwin/order/listDealerImage.jsp?orderUuid='+$("#uuid").val();
		} else
		{
			alert(resultData.errorMsg);
		}
	});
}
